$(function(){
	$("#loading-first").fadeOut(500);
	var url={
		api:{
			delete:"{:url('adminapi/Driver/delete')}",
			list:"{:url('adminapi/Driver/driverList')}",
		},
		path:{
			detail:"{:url('/admin/driver/detail')}"
		}
		
		
	}
	var page=1;
	var main=[[ //表头
		 {type:'checkbox', fixed: 'left'}
        ,{field: 'name', title: '姓名',  sort: true, fixed: 'left'}
        ,{field: 'phone', title: '电话',  sort: true}
        ,{field: 'company_name',width:200, title: '所属公司'} 
        ,{title: '关联车辆', toolbar:'#relevance'} 
        ,{field: 'driving_age', title: '驾龄'}
//      ,{field: 'balance', title: '余额', sort: true}
        ,{field:'star', title:'星级'}
        ,{field:'work_state2', title:'司机状态'}
        ,{field:'state2', title:'状态'}
        ,{ title: '操作',width:200, toolbar:'#operation',fixed: 'right'}
    ]];
    var data={};
    data.page=page;
    data.pagenum=10;
    var where_json={};
    {if $Think.get.work_state}
		where_json.work_state=['in','{$Think.get.work_state}']
	{/if}
	{if $Think.get.company_name}
		where_json.company_name=['like','%{$Think.get.company_name}%']
	{/if}
	{if $Think.get.name}
		where_json.name=['like','%{$Think.get.name}%']
	{/if}
	{if $Think.get.phone}
		where_json.phone=['like','%{$Think.get.phone}%']
	{/if}
	{if $Think.get.number}
		where_json.number=['like','%{$Think.get.number}%']
	{/if}
	{if $Think.get.order_start}
		where_json.apply_time=['between time',['{$Think.get.order_start}','{$Think.get.order_end}']]
	{/if}
	where_json.company_id=['in',$('.company_all_id').val()]
	data.where_json=JSON.stringify(where_json)
    ajaxdriver(data)
    function ajaxdriver(data){
    	console.log(data)
    	Load()
    	$.ajax({
    		type:'post',
    		url:url.api.list,
    		data:data,
    		success:function(res){
    			Load(1)
    			if(res.code==1){
    			console.log(res)
    			res=driverstate(res)
    			 var table = layui.table
					  ,form = layui.form;
    			table.render({
				  	elem: '#driver'
				    ,cellMinWidth: 80
				    ,cols: main
					,data: res.data
					,even: true
				});
				$('.pagination').html(pagechang1(res.count, 10, page))
				//删除多个
				
				$(".delete-multiple").on('click',function(){
					var driver_ids='';
					var checkStatus = table.checkStatus('driver'); //test即为基础参数id对应的值
 					console.log(checkStatus.data) //获取选中行的数据
					console.log(checkStatus.data.length) //获取选中行数量，可作为是否有选中行的条件
					console.log(checkStatus.isAll ) //表格是否全选
					for(var dei=0;dei<checkStatus.data.length;dei++){
						if(dei==0){
							driver_ids+=checkStatus.data[dei].driver_id
						}else{
							driver_ids+=(","+checkStatus.data[dei].driver_id)
						}
					}
					console.log(driver_ids)
					layer.open({
						type: 1,
						title: false,
						id: "delete" ,
						content: "<div class='am-vertical-align-middle' style='width:100%;text-align:center'>是否删除</div>",
						btn: ['确定', '取消'],
						btnAlign: 'c' //按钮居中
							,
						shade: 0 //不显示遮罩
							,
						yes: function() {
							var deleteload = layer.load(1, {
								shade: [0.1, '#fff'] //0.1透明度的白色背景
							});
							$.ajax({
								method: "POST",
								url:url.api.delete,
								data: {
									driver_ids:driver_ids
								},
								success: function(res) {
									console.log(res)
									if(res.code == 1) {
										layer.close(deleteload);
										layer.closeAll();
										Msg('删除成功')
										ajaxdriver(data)
									} else  {
										layer.close(deleteload);
										Err(res.msg ? res.msg : '操作失败，稍候再试');
									}
		
								}
							});
		
						},
						btn2: function() {
							layer.closeAll();
				}
			});
				});
				table.on('tool(driver)', function(obj){
					  var data1 = obj.data; //获得当前行数据
					  var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
					  var tr = obj.tr; //获得当前行 tr 的DOM对象
					 
					  if(layEvent === 'detail'){ //查看
					  	
					  	window.location.href = url.path.detail+"?driver_id="+data.driver_id
					  } else if(layEvent === 'del'){ //删除
					    layer.confirm('是否删除', function(index){
					    	Load()
					    	 $.ajax({
							method: "POST",
							url: url.api.delete,
							data: {
								driver_ids:data1.driver_id
							},
							success: function(res) {
								if(res.code == 1) {
									ajaxdriver(data)
									 layer.close(index);
									 Load(1)
									
								} else {
									 layer.close(index);
									 Load(1)
									Err(res.msg ? res.msg : '操作失败，稍候再试');
								}
	
							},
							error:function(){
								Load(1)
								Err('请求失败')
							}
						});
					      layer.close(index);
					    });
					  } else if(layEvent === 'edit'){ //编辑
					    //do something
					   	window.location.href = " {:url('edit')}?driver_id="+data1.driver_id+"&page="+page
					    //同步更新缓存对应的值
					   
					  } else if(layEvent === 'relevance'){ //编辑
					    //do something
					   	window.location.href = " {:url('relevance')}?driver_id="+data1.driver_id+"&page="+page
					    //同步更新缓存对应的值
					   relevance
					  }else if(layEvent === 'forbidden'){
					  	layer.confirm('是否禁用', function(index){
					    	Load()
					    	 $.ajax({
							method: "POST",
							url: "{:url('/unit/driver/state')}",
							data: {
								id:data1.driver_id,
								state: 9
							},
							success: function(res) {
								if(res.code == 1) {
									ajaxdriver(data)
									 layer.close(index);
									 Load(1)
									
								} else {
									 layer.close(index);
									 Load(1)
									Err(res.msg ? res.msg : '操作失败，稍候再试');
								}
	
							},
							error:function(){
								Load(1)
								Err('请求失败')
							}
						});
					      layer.close(index);
					    });
					  }else if(layEvent === 'startusing'){
					  	layer.confirm('是否启用', function(index){
					    	Load()
					    	 $.ajax({
							method: "POST",
							url: "{:url('/unit/driver/state')}",
							data: {
								id:data1.driver_id,
								state: 1
							},
							success: function(res) {
								if(res.code == 1) {
									ajaxdriver(data)
									 layer.close(index);
									 Load(1)
									
								} else {
									 layer.close(index);
									 Load(1)
									Err(res.msg ? res.msg : '操作失败，稍候再试');
								}
	
							},
							error:function(){
								Load(1)
								Err('请求失败')
							}
						});
					      layer.close(index);
					    });
					  }
				});
				$(".export").on('click',function(){
					
					var tddata2=[
					{key:'name'},
					{key:'phone'},
					{key:'company_name'},
					{key:'driving_age'},
					{key:'star'},
					{key:'work_state'},
					{key:'state'}
					];
					
					exports(res,tddata2,data,"{:url('adminapi/Driver/downLoadDriverList')}")
				})
				$('.laquo').on('click',function(){
					console.log(data)
					page-=1;
					data.page=page
					ajaxdriver(data)
				})
				$('.pageli').on('click',function(){
					console.log(data)
					page=$(this).html()
					data.page=page
					ajaxdriver(data)
				})
				//下一页
				$('.raquo').on('click',function(){
					page+=1;
					data.page=page
					ajaxdriver(data)
				});

				}else{
					Err(res.msg)
				}
    		},
    		error:function(){
    			Load(1)
    			Err('请求失败')
    		}
    	})
    }
	//删除
	$('.delete-list').on('click',function(){
		console.log(1)
		popup(this,"{:url('delete')}");
		
	});
	//启用
	$('.startusing').on('click',function(){
		popup(this,"{:url('state')}");
		
	});
	//禁用
	$('.forbidden').on('click',function(){
		popup(this,"{:url('state')}");
		
	});
	//全选
	$('.check-all').on('click',function(){
		checkall(this)
	});
	$('.check-list').on('click',function(){
		checkall(this)
	});
	
	
	  
	 
//	$('.unbind').on('click', function() {
//		$('.driver_id').val($(this).closest('tr').data('id'))
//		choosecar("{:url('car/index',['op'=>'unbind'])}?driver_id="+$(this).closest('tr').data('id'))
//	});
//	$('.bind').on('click', function() {
//		$('.driver_id').val($(this).closest('tr').data('id'));
//		var id = $(this).closest('tr').data('id');
//		choosecar("{:url('car/index',['op'=>'bind'])}?driver_id="+$(this).closest('tr').data('id'));
//	});
	$('.bind').on('click', function() {
		choosecar("{:url('car/relevance',['op'=>'unbind'])}?driver_id="+$(this).closest('tr').data('id'))
	});
	$('.choose').on('click', function() {
		var index = parent.layer.getFrameIndex(window.name);
		parent.$('.driver').val($(this).data('name'));
		parent.$('.driver_id').val($(this).data('id'));
		parent.layer.close(index);

	});
	$('.car_rental').on('click', function() {
		choosecar("{:url('rent/index',['op'=>'search'])}")
	});
})
